package com.coding.service.impl;

import com.coding.base.pojo.BaseModel;
import com.coding.base.pojo.PageQueryModel;
import com.coding.base.util.CreateOssUtil;
import com.coding.dao.GroupDao;
import com.coding.dao.GroupwithDao;
import com.coding.dao.OssImagesDao;
import com.coding.pojo.Group;
import com.coding.pojo.Groupwith;
import com.coding.pojo.OssImages;
import com.coding.service.GroupService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;


/**
 * (Group)表服务实现类
 *
 * @author makejava
 * @since 2020-07-31 14:25:08
 */
@Service
public class GroupServiceImpl implements GroupService {
    @Autowired
    OssImagesDao ossImagesDao;
    @Autowired
    GroupDao groupDao;
    @Autowired
    GroupwithDao groupwithDao;

    @Override
    public void selectAllbypage(PageQueryModel pageQueryModel, BaseModel baseModel) throws Exception {
        PageHelper.startPage(pageQueryModel.getPage(), pageQueryModel.getSize());
        List<Group> groupList = groupDao.queryAllPage();
        PageInfo pageInfo = new PageInfo(groupList);
        groupList = pageInfo.getList();
        if (groupList.size() == 0) {
            if (pageInfo.getPageNum() > 1) {
                baseModel.setFailure("已经没有下一页了");
            } else {
                baseModel.setOther(2, "没有符合条件的圈子");
            }
            return;
        }
        baseModel.setSucceed("查询成功，符合条件的圈子共" + groupList.size() + "个", groupList);
    }

    @Override
    public void selectAllbypage(int user_id, PageQueryModel pageQueryModel, BaseModel baseModel) throws Exception {
        PageHelper.startPage(pageQueryModel.getPage(), pageQueryModel.getSize());
        List<Group> groupList = groupDao.queryAllPage();
        for (Group group : groupList
        ) {
            Groupwith groupwith = groupwithDao.queryById(user_id, group.getGroupId());
            if (groupwith != null) {
                group.setGroupMore1("已加入");
            }
        }
        PageInfo pageInfo = new PageInfo(groupList);
        groupList = pageInfo.getList();
        if (groupList.size() == 0) {
            if (pageInfo.getPageNum() > 1) {
                baseModel.setFailure("已经没有下一页了");
            } else {
                baseModel.setOther(2, "没有符合条件的圈子");
            }
            return;
        }
        baseModel.setSucceed("查询成功，符合条件的圈子共" + groupList.size() + "个", groupList);
    }

    @Override
    public void selectbygroup_id(int group_id, BaseModel baseModel) throws Exception {
        Group group = groupDao.queryById(group_id);
        if (group != null) {
            baseModel.setSucceed("查询圈子信息成功", group);
        } else {
            baseModel.setFailure("查询圈子信息失败");
        }
    }

    @Override
    public void selectByGroupName(int user_id, String group_name, PageQueryModel pageQueryModel, BaseModel baseModel) throws Exception {
        PageHelper.startPage(pageQueryModel.getPage(), pageQueryModel.getSize());
        List<Group> groupList = groupDao.queryByGroupName(group_name);
        for (Group group : groupList
        ) {
            Groupwith groupwith = groupwithDao.queryById(user_id, group.getGroupId());
            if (groupwith != null) {
                group.setGroupMore1("已加入");
            }
        }
        PageInfo pageInfo = new PageInfo(groupList);
        groupList = pageInfo.getList();
        if (groupList.size() == 0) {
            if (pageInfo.getPageNum() > 1) {
                baseModel.setFailure("已经没有下一页了");
            } else {
                baseModel.setOther(2, "没有符合条件的圈子");
            }
            return;
        }
        baseModel.setSucceed("查询成功，符合条件的圈子共" + groupList.size() + "个", groupList);
    }

    @Override
    public void selectbytag(String tag, PageQueryModel pageQueryModel, BaseModel baseModel) throws Exception {
        PageHelper.startPage(pageQueryModel.getPage(), pageQueryModel.getSize());
        List<Group> groupList = groupDao.queryAllbytag(tag);
        PageInfo pageInfo = new PageInfo(groupList);
        groupList = pageInfo.getList();
        if (groupList.size() == 0) {
            if (pageInfo.getPageNum() > 1) {
                baseModel.setFailure("已经没有下一页了");
            } else {
                baseModel.setOther(2, "没有符合条件的圈子");
            }
            return;
        }
        baseModel.setSucceed("查询成功，符合条件的圈子共" + groupList.size() + "个", groupList);
    }

    @Override
    public void insertnewgroup(Group group, MultipartFile file, BaseModel baseModel) throws Exception {
        if (file == null || file.getSize() <= 0) {
            baseModel.setFailure("file不能为空");
            return;
        }
        //上传图片至oss
        OssImages ossImages = CreateOssUtil.createOss(file);
        if (ossImages == null || ossImages.getImage_url() == null) {
            baseModel.setFailure("上传图片失败");
            return;
        }
        if (groupDao.insert(group) == 1) {
            Group newgroup = groupDao.queryById(group.getGroupId());
            if (newgroup == null) {
                baseModel.setFailure("新增圈子失败");
                return;
            }
            baseModel.setSucceed("新增圈子成功", newgroup);
        } else {
            baseModel.setFailure("新增圈子失败");
        }

    }

    @Override
    public void updategroupintro(Group group, BaseModel baseModel) throws Exception {
        Group newgroup = groupDao.queryById(group.getGroupId());
        if (newgroup == null) {
            baseModel.setFailure("无法更新，查无此圈子");
            return;
        }

        if (groupDao.update(group) == 1) {
            Group group1 = groupDao.queryById(group.getGroupId());
            baseModel.setSucceed("成功更新圈子", group1);
        } else {
            baseModel.setFailure("更新圈子失败");
        }
    }

    @Override
    public void selectUserJoin(int user_id, BaseModel baseModel) throws Exception {
        Groupwith testGroup = new Groupwith();
        testGroup.setUserId(user_id);
        List<Groupwith> groupwithList = groupwithDao.queryAll(testGroup);
        if (groupwithList == null) {
            baseModel.setFailure("该用户没有加入圈子");
            return;
        }
        List<Group> groupList = new ArrayList<>();
        for (Groupwith groupwith : groupwithList
        ) {
            Group group = groupDao.queryById(groupwith.getGroupId());
            if (group != null) {
                groupList.add(group);
            }
        }
        if (groupList.size() > 0) {
            baseModel.setSucceed("成功获得用户已加入圈子列表", groupList);
        } else {
            baseModel.setFailure("获得用户已加入圈子失败");
        }
    }
}